<?php
/**
 * 基本配置
 */
class ControllerConfigCommon extends Controller {

	private $error = array();

	protected function init(){
		$this->load_language('config/common');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('config/common');
	}

	public function index() {
		$this->init();
		$this->getList();
	}

	public function insert() {
		$this->init();
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_config_common->addCommon($this->request->post);
			$this->redirectToList();
		}
		$this->getForm();
	}

	public function update() {
		$this->init();

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_config_common->editCommon($this->request->get['common_id'], $this->request->post);
			$this->redirectToList();
		}
		$this->getForm();
	}

	public function delete() {
		$this->init();

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $common_id) {
				$this->model_config_common->deleteCommon($common_id);
			}

			$this->redirectToList();
		}

		$this->getList();
	}


	private function getList() {
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$data = array(
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		
		
		$this->data['commons'] = array();
		
	    $user_total = $this->model_config_common->getTotalCommons();
		
		$results = $this->model_config_common->getCommons($data);
		
		foreach ($results as $result) {
			$action = array();
			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('config/common/update', 'token=' . $this->session->data['token'] . '&common_id=' . $result['common_id'] , 'SSL')
			);
					
      		$this->data['commons'][] = array(
				'common_id'    => $result['common_id'],
      			'common_name'    => $result['common_name'],
				'url'   => $result['url'],
				'token'     => $result['token'],
      			'appid'     => $result['appid'],
      			'appsecret'     => $result['appsecret'],
				'action'     => $action,
      			'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
      			'selected'   => isset($this->request->post['selected']) && in_array($result['id'], $this->request->post['selected']),
			);
		}	
		
		$url = "";
		
		if(isset($this->request->get['common_id']))
		{
			$url = "&common_id="+$this->request->get['common_id'];
		}
		
		if (isset($this->request->post['status'])) {
      		$this->data['status'] = $this->request->post['status'];
    	} elseif (isset($user_info)) {
			$this->data['status'] = $user_info['status'];
		} else {
      		$this->data['status'] = 0;
    	}
		
		$this->data['insert'] = $this->url->link('config/common/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['delete'] = $this->url->link('config/common/delete', 'token=' . $this->session->data['token'].$url, 'SSL');
		
		$pagination = new Pagination();
		$pagination->total = $user_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('config/common', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();
		
		$this->template = 'config/common_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		

		$this->response->setOutput($this->render());
	}

	private function getForm() {

		if (isset($this->error['url'])) {
			$this->data['error_url'] = $this->error['url'];
		} else {
			$this->data['error_url'] = '';
		}

		if (isset($this->error['error_token'])) {
			$this->data['error_token'] = $this->error['token'];
		} else {
			$this->data['error_token'] = '';
		}

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		$this->data['url'] = "";
		$this->data['token'] = "";
		$this->data['appid'] = "";
		$this->data['appsecret'] = "";
		$this->data['status'] = "";
		$this->data['common_name'] = "";
		if (isset($this->request->get['common_id'])) {
			$commonInfo = $this->model_config_common->getCommon($this->request->get['common_id']);
			if(isset($commonInfo))
			{
				$this->data['url'] = $commonInfo['url'];
				$this->data['token'] = $commonInfo['token'];
				$this->data['appid'] = $commonInfo['appid'];
				$this->data['appsecret'] = $commonInfo['appsecret'];
				$this->data['status'] = $commonInfo['status'];
				$this->data['common_name'] = $commonInfo['common_name'];
			}
		}
		
		$url = "";
		
		if(isset($this->request->get['common_id']))
		{
			$url = $url."&common_id=".$this->request->get['common_id'];
		}
		
		if (isset($this->request->get['common_id'])) {
			$this->data['action'] = $this->url->link('config/common/update', 'token=' . $this->session->data['token'].$url , 'SSL');
		}else{
			$this->data['action'] = $this->url->link('config/common/insert', 'token=' . $this->session->data['token'].$url , 'SSL');
		}
		
		$this->data['cancel'] = $this->url->link('config/common', 'token=' . $this->session->data['token'], 'SSL');
		
		
		$this->template = 'config/common_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		

		$this->response->setOutput($this->render());
		
	}

	private function validateForm() {
			
		if (!$this->error) {
			return TRUE;
		} else {
			$this->error['warning'] = $this->language->get('error_required_data');

			return FALSE;
		}
	}

	private function validateDelete() {
		/*if (!$this->user->hasPermission('modify', 'catalog/article')) {
			$this->error['warning'] = sprintf($this->language->get('error_permission'),$this->language->get('heading_title'));
		}
		*/
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function category() {
		$this->load->model('catalog/article');

		if (isset($this->request->get['category_id'])) {
			$category_id = $this->request->get['category_id'];
		} else {
			$category_id = 0;
		}

		$article_data = array();

		$results = $this->model_catalog_article->getArticleByCategoryId($category_id);

		foreach ($results as $result) {
			$article_data[] = array(
				'article_id' => $result['article_id'],
				'title'       => $result['name']
			);
		}

		$this->load->library('json');

		$this->response->setOutput(Json::encode($article_data));
	}

	public function related() {
		$this->load->model('catalog/article');

		if (isset($this->request->post['article_related'])) {
			$article = $this->request->post['article_related'];
		} else {
			$article = array();
		}

		$article_data = array();

		foreach ($article as $article_id) {
			$article_info = $this->model_catalog_article->getArticleName($article_id);

			if ($article_info) {
				$article_data[] = array(
					'article_id' => $article_info['article_id'],
					'title'       => $article_info['name']
				);
			}
		}

		$this->load->library('json');

		$this->response->setOutput(Json::encode($article_data));
	}

	private function redirectToList(){
		$this->session->data['success'] = sprintf($this->language->get('text_success'),$this->language->get('heading_title'));

		$url=$this->getUrlParameters();

		$this->redirect(HTTPS_SERVER . 'index.php?route=config/common&token=' . $this->session->data['token'] . $url);
	}

	private function getCommonUrlParameters(){
		$url = '';

		if (isset($this->request->get['filter_title'])) {
			$url .= '&filter_title=' . $this->request->get['filter_title'];
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['filter_date_add'])) {
			$url .= '&filter_date_add=' . $this->request->get['filter_date_add'];
		}

		if (isset($this->request->get['filter_article_category_id'])) {
			$url .= '&filter_article_category_id=' . $this->request->get['filter_article_category_id'];
		}

		return $url;
	}

	public function getUrlParameters(){
		$url = '';

		$url=$this->getCommonUrlParameters();

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		return $url;
	}

}
?>